package com.px.project.module.ht.service.impl;

import com.px.common.config.exception.BusinessException;
import com.px.common.dto.BaseQueryDto;
import com.px.project.module.ht.dto.CameraUpdateDto;
import com.px.project.module.ht.entity.Camera;
import com.px.project.module.ht.enums.ManagementExceptionEnum;
import com.px.project.module.ht.mapper.CameraMapper;
import com.px.project.module.ht.service.CameraService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cn.hutool.core.util.ObjectUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * 服务实现类
 *
 * @author 品讯科技
 * @since 2023-10-10
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class CameraServiceImpl extends ServiceImpl<CameraMapper, Camera> implements CameraService {
    @Override
    public Page<Camera> page(BaseQueryDto<Camera> pageDto, Camera camera) {
        LambdaQueryWrapper<Camera> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.setEntity(camera);
        return super.page(pageDto.toPage(), queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveOne(Camera camera) {
        if (ObjectUtil.isNotEmpty(camera.getCamera())) {
            QueryWrapper<Camera> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(Camera::getCamera, camera.getCamera());
            Camera findCamera = super.getOne(queryWrapper);
            if (ObjectUtil.isNotEmpty(findCamera)) {
                throw new BusinessException(ManagementExceptionEnum.CAMERA_NAME_EXISTS, findCamera.getCamera());
            }
        }
        return super.save(camera);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateOfOne(CameraUpdateDto camera) {
        Camera cameraById = super.getById(camera.getId());
        if (ObjectUtil.isEmpty(cameraById)) {
            throw new BusinessException(ManagementExceptionEnum.CAMERA_NO_EXISTS);
        }
        if (ObjectUtil.isNotEmpty(camera.getCamera())) {
            QueryWrapper<Camera> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(Camera::getCamera, camera.getCamera());
            Camera findCamera = super.getOne(queryWrapper);
            if (ObjectUtil.isNotEmpty(findCamera) && ObjectUtil.notEqual(findCamera.getId(), camera.getId())) {
                throw new BusinessException(ManagementExceptionEnum.CAMERA_NAME_EXISTS, findCamera.getCamera());
            }
        }
        return super.updateById(camera);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteOne(Long id) {
        Camera camera = super.getById(id);
        if (ObjectUtil.isEmpty(camera)) {
            throw new BusinessException(ManagementExceptionEnum.CAMERA_NO_EXISTS);
        }
        return super.removeById(id);
    }
}